package Thread;

//join方法
//主线程等待t1和t2
//两个join写在main线程中,所以是main线程等待t1和t2
public class Demo15 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 3; i++) {
                System.out.println("hello t1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("t1结束");
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 3; i++) {
                System.out.println("hello t2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("t2结束");
        });

        t1.start();
        t2.start();
        System.out.println("主线程等待开始");
        t1.join();
        t2.join();
        System.out.println("主线程等待结束");
        /*
        如果没有join,开始和结束这两句话就几乎同时打印
        1.打印主线程开始结束
        2.打印hello t1
        3.打印hello t2

         这里的1  2  3这三个日志的顺序是不确定的
         不确定哪个线程先执行,哪个后执行

         但是由于t1和t2有创建的开销,所以一般来说是1先打印,2和3的顺序不确定因素会更多一点
         */
    }
}
